/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//hgfhfgjgfdjfjfjf
package com.fitcoders.inscripcion.controller;

import com.fitcoders.inscripcion.model.Curso;
import com.fitcoders.inscripcion.model.Persona;
import com.fitcoders.inscripcion.service.CursoService;
import com.fitcoders.inscripcion.service.PersonaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Max
 */
@Controller
public class InscripcionController {
    
    @Autowired
    PersonaService personaService;
    @Autowired
    CursoService cursoService;
    
    @RequestMapping(value = "/inscripciones", method = RequestMethod.GET)
    public String listPersonas(Model model) throws Exception {
        Persona persona = new Persona();
        persona.setIdCurso(new Curso());
        model.addAttribute("inscripcion", persona);
        model.addAttribute("listaCursos", cursoService.list("from Curso c", null, null));
        model.addAttribute("personasInscritas", personaService.list("from Persona p", null, null));
        return "inscripcion";
    }
    
    @RequestMapping(value = "/process-inscripcion")
    public String processPerson(@ModelAttribute("inscripcion") Persona persona) throws Exception {

        if (persona.getId() == null) {
            //new person, add it
            personaService.save(persona);
        } else {
            //existing person, call update
            personaService.update(persona);
        }
        
        return "redirect:/inscripciones.htm";
    }
    
    @RequestMapping("/editInscr/{id}")
    public String editPerson(@PathVariable("id") int id, Model model) throws Exception {
        Persona persona = personaService.getByHQL("from Persona p where p.id="+id, null);
        model.addAttribute("inscripcion",  persona);
        model.addAttribute("listaCursos", cursoService.list("from Curso c", null, null));
        model.addAttribute("personasInscritas", personaService.list("from Persona p", null, null));
        
        return "inscripcion";
    }
    
    /**
     * Los que no pagaron
     */
    @RequestMapping(value = "/noPagaron", method = RequestMethod.GET)
    public String listNoPagaron(Model model) throws Exception {
        model.addAttribute("personasInscritas", personaService.list("from Persona p where p.pago=false", null, null));
        return "nopagaron";
    }
    
}
